package com.daofree.dao.impl;

import com.daofree.dao.ItemsDao;
import com.daofree.domain.Items;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ItemsDaoImpl
 * @Description: 实现类-数据库连接4要素
 * @Author DaoTianXia
 * @Date 2020-09-22-19:40
 * @Version V1.0
 **/
public class ItemsDaoImpl implements ItemsDao {
    public List<Items> findAll() throws Exception {
        List<Items> list = new ArrayList<Items>();
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // "root","123"
            connection = DriverManager.getConnection("jdbc:mysql://192.168.112.128:3306/maven","root","123");
            pst = connection.prepareCall("SELECT * FROM items");
            rs = pst.executeQuery();
            // 把数据库结果集转成java的list集合

            while (rs.next()){
                Items items = new Items();
                items.setId(rs.getInt("id"));
                items.setName(rs.getString("name"));
                list.add(items);
            }
        }finally {
            rs.close();
            pst.close();
            connection.close();
        }

        return list;
    }
}
